#!/bin/bash

cd /home/qie_cwar_1/code/run

PATH_ID=1
SET_ID=1

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
no="\033[0m"

svr_list=`echo /home/qie_cwar_${PATH_ID}/code/run/{${SET_ID}trfsvrd,${SET_ID}dbsvrd,${SET_ID}logsvrd,${SET_ID}gmsvrd}`
cd /home/qie_cwar_${PATH_ID}/code/run

rm -rf ./cid/5.${SET_ID}.cid
rm -rf ./cid/6.${SET_ID}.cid
rm -rf ./cid/7.${SET_ID}.cid
rm -rf ./cid/9.${SET_ID}.cid

rm -rf ./tid/5.${SET_ID}.tid
rm -rf ./tid/6.${SET_ID}.tid
rm -rf ./tid/7.${SET_ID}.tid
rm -rf ./tid/9.${SET_ID}.tid

if [ ! -s /home/qie_cwar_${PATH_ID}/code/run/mon_big_set_svr.pid ]
then
   if pgrep -U df -f mon_big_set_svr.sh &> /dev/null
   then
        kill -9 `pgrep -f  mon_big_set_svr.sh`
      echo echo -e "$green $(date) kill mon process ok $no"
   else
      echo -e "$yellow $(date) the mon process not running $no"
   fi
else
     kill -9 `cat /home/qie_cwar_${PATH_ID}/code/run/mon_big_set_svr.pid` &> /dev/null  
   sleep 1
   if pgrep -U df  -f mon_big_set_svr.sh &> /dev/null
   then
      echo -e "$red $(date) the mon process stop err $no"
   else
      echo -e "$green $(date) kill mon process ok $no"
      rm -f /home/qie_cwar_${PATH_ID}/code/run/mon_big_set_svr.pid
   fi
fi 

stop()
{
list=$1
for i in $svr_list
do
   cd $i
   j=`echo ${i##*/}` 
   m=$j
   if [ -s ${m}.pid ]
   then 
      read SVC_PID < ${m}.pid
   else
      read SVC_PID <<< 0
   fi
    
   if [ -s ${SET_ID}commsvrd.pid ]
   then
      read com < ${SET_ID}commsvrd.pid
   else
      read com <<< 0  
   fi
    
   if [ $SVC_PID = 0 ]
   then
      if pgrep -U df -f  "$m.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "$m.*$j"`
         echo -e "$green $(date) kill  $j $m ok  $no"
      else
         echo -e "$yellow $(date) $j $m not running $no"
      fi
   else
        kill -3 $SVC_PID &> /dev/null
      while true
      do
        kill -0 $SVC_PID  &> /dev/null ||  break 
      sleep 1
      done
      
      sleep 1
      if pgrep -U df -f  "$m.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "$m.*$j"`
         echo -e "$green $(date) kill  $j  $m ok $no"
         rm -f ${m}.pid
      else
         echo -e "$green $(date) SIGQUIT $j  $m ok  $no"
         rm -f ${m}.pid
      fi
   fi

   if [ $com = 0 ]
   then  
      if pgrep -U df -f  "${SET_ID}commsvrd.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df -f  "${SET_ID}commsvrd.*$j"`
         echo -e "$green $(date) kill  $j ${SET_ID}commsvrd ok  $no" 
      else
         echo -e "$yellow $(date) $j ${SET_ID}commsvrd not running $no"
      fi
   else
        kill -3 $com &> /dev/null
      sleep 1
      if pgrep -U df  -f  "${SET_ID}commsvrd.*$j" &> /dev/null
      then
           kill -9 `pgrep -U df  -f  "${SET_ID}commsvrd.*$j"`
         echo -e "$green $(date) kill  $j  ${SET_ID}commsvrd ok  $no"
         rm -f ${SET_ID}commsvrd.pid
      else
         echo -e "$green $(date) SIGQUIT $j ${SET_ID}commsvrd $no"
         rm -f ${SET_ID}commsvrd.pid
     fi
   fi
done
}

stop "$svr_list"

mmapdir=`echo /home/mmap/qie_big_set_${PATH_ID}`
umount $mmapdir
